<?php
/**
 * 权限菜单模型
 * @author Administrator
 *
 */
namespace app\home\model;
use think\Model;


class Authrule extends Model {
	protected $table = 'hs_auth_rule';
	protected $resultSetType = 'collection';

    protected $_validate = array(
        array('name','require','[链接地址] 不能为空！',1),
        array('name','','[链接地址]已经存在！',0,'unique',1), // 在新增的时候验证name字段是否唯一
        array('title','require','[名称] 不能为空！',1),
    );

    /**
	 * 获取带条件的菜单权限结点
	 * @param unknown $map
	 * @return unknown
	 */
	public function lst($map=array()) {
		$list = $this->where($map)->order("fid asc,sort asc")->select()->toArray();
		return $list;
	}
	
	/**
	 * 获取单条数据
	 * @param unknown $map
	 * @return Ambigous <\think\static, NULL>
	 */
	public function getone($map){
		return $this->get($map)->toArray();
	}

	
	/**
	 * 
	 * @param unknown $arr
	 * @param number $fid
	 * @return multitype:multitype:unknown
	 * remake:递归查询
	 */
	public function FixNode($arr,$fid=0)
	{
		$nodes = array();
		foreach($arr as $key=>$row)
		{
			unset($row["haschild"]);
			unset($row["condition"]);
			unset($row["remark"]);
			unset($row["weigh"]);
			unset($row["status"]);
			
			if($row["fid"] == $fid)
			{
				$row["children"]= $this->FixNode($arr,$row['id']);
				$nodes[] =$row;
				unset($arr[$key]);
			}
		}
		return $nodes;
	}

	/**ztree_json
	 * 获取带所有子结点的分类
	 * @return unknown
	 */
	public function ztree(){
		$data=$this->lst();
		

		$json=array();
		foreach($data as $key=>$val){
				
			$json[$key]["id"]=$val["id"];
			$json[$key]["name"]=$val["title"];
			$json[$key]["fid"]=$val["pid"];
			$json[$key]["open"]=true;
		}
		return $json;
	}
	
	/**
	 * 获取单条数据
	 * @param unknown $map
	 * @return Ambigous <\think\static, NULL>
	 */
	public function savedata($data,$map){
		print_r($map);
		print_r($data);
		 echo $this->where($map)->update($data);
		 return;
	}
	
	/**
	 * 获取带所有子id的节点
	 * @return unknown
	 */
	public function lst_sub(){
		$data=$this->lst();

		foreach($data as $key=>$val){
			$subid=getSonNode($data,$val["id"],$SonNode = array());;
			$data[$key]["subid"]=$subid;
		}
		return $data;
	}

    public function getIsmenuAttr($value)
    {
        return $value == 0 ? false : true;
    }
}
